/*
 * @Author: C.
 * @Date: 2023-04-07 14:29:34
 */
// interface IItem {
//   label: string;
//   field: string;
//   count: number;
// }
// interface IMap {
//   [key: string]: IItem;
// }
function handleFieldList(fieldList) {
  const res = {};
  fieldList.forEach((_, index) => {
    if (index % 2 == 1) {
      const labelarr = fieldList[index - 1].split('\t');
      const fieldarr = fieldList[index].split('\t');
      if (labelarr.length !== fieldarr.length) {
        console.log(labelarr.length, fieldarr.length);
        console.log('不良数据', `col:${index}`);
      }
      fieldarr.forEach((field, idx) => {
        if (Reflect.has(res, field)) {
          res[field] = {
            ...res[field],
            count: res[field].count + 1,
          };
        } else {
          res[field] = {
            label: labelarr[idx],
            field,
            count: 1,
          };
        }
        if (field == '') {
          console.log(labelarr[idx], `[row:${index},col:${idx}]`);
        }
      });
    }
  });
  return res;
}
/**
 * @description: 根据字段表生成字段集合
 * @param {*} fieldList
 * @param {*} frequency
 * @return {*}
 */
function genField(fieldList, frequency = 1) {
  const fieldMap = handleFieldList(fieldList);
  const fieldArr = [];
  for (const key in fieldMap) {
    if (fieldMap[key].count >= frequency) {
      const obj = fieldMap[key];
      fieldArr.push({
        label: obj.label,
        field: obj.field,
      });
    }
  }
  // 多语言
  console.log('/////////////////中文///////////////////');
  console.log('{');
  for (const item of fieldArr) {
    console.log(`  ${item.field} : '${item.label}',`);
  }
  console.log('}');
  console.log('/////////////////英文////////////////////////');
  console.log('{');
  for (const item of fieldArr) {
    console.log(`${item.field} : '${item.field}',`);
  }
  console.log('}');

  return fieldArr;
}
/**
 * @description: 根据字段集合生成对象的组件字段
 * @param {*} fieldList
 * @param {*} type
 * @return {*}
 */
function genFieldArr(fieldList, frequency = 1, type = 'table') {
  const fieldArr = genField(fieldList, frequency);
  let Itype = '';
  fieldArr.forEach((item) => {
    Itype += `|"${item.field}"`;
  });
  // 打印类型
  console.log(Itype);
  // 打印字段列表
  if (type === 'table') {
    console.log(
      fieldArr.map((item) => {
        return {
          dataIndex: item.field,
          title: item.label,
        };
      }),
    );
  } else if (type === 'form') {
    console.log(
      fieldArr.map((item) => {
        return {
          field: item.field,
          label: `t("commonField.${item.field}")`,
          component: 'Input',
        };
      }),
    );
  } else {
    console.log(
      fieldArr.map((item) => {
        return {
          field: item.field,
          label: `t("commonField.${item.field}")`,
        };
      }),
    );
  }
}
//托盘Id	工序状态
console.log(
  genFieldArr(
    [
      '无料	待加工	已加工	加工中	未知	错误	异常',
      'Empty	ToBeProcessed	Processed	Processing	Unknown	Error	Abnormal',
    ],
    1,
    'table',
  ),
);
